package com.sq.foursm.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sq.foursm.core.RealmUtil;
import com.sq.foursm.service.UserService;


/**
 * 用户登陆访问模块
 * @author SQ
 * 2017年7月4日
 * 下午1:45:33
 */
@Controller
public class LoginAction {
	
	@Resource
	private UserService userService ;
	
	@RequestMapping("/login")
	public String login(HttpServletResponse response,HttpServletRequest Request ,String username,String password){
		System.out.println(new SimpleHash("MD5", "11111", "shangqi", 3));
		
		Subject subject = SecurityUtils.getSubject();
		if(username==null||username==null||password==""||password==null){
			return "index";
		}
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		try {
			subject.login(token);
		} catch (Exception e) {
		    System.out.println("登陆失败");
		    return "index";
		}
		return "test";
	}
	/**
	 * 使用自动登陆的过滤器
	 * @param Request
	 * @return
	 * @throws Exception
	 */
//	@RequestMapping("/login")
//	public String login(HttpServletRequest Request) throws Exception{
//		
//		System.out.println(Request.getParameter("username"));
//		String exceptionClassName =(String) Request.getParameter("shiroLoginFailure");
//		System.out.println(exceptionClassName);
//		if(exceptionClassName!=null){
//			if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
//				return "login";
//			//	throw new Exception("账号不存在");
//				
//			} else if (IncorrectCredentialsException.class.getName().equals(
//					exceptionClassName)) {
//				return "login";
//			//	throw new Exception("用户名/密码错误");
//			} else if("randomCodeError".equals(exceptionClassName)){
//				return "login";
//			//	throw new Exception("验证码错误");
//			} else{
//				//throw new Exception();//最终在异常处理器生成未知错误
//			}
//		}
//
//		return "login";
//		
//	}
	@RequestMapping("gotouser")
	@RequiresPermissions("user:create")
	public String gotouser(){
	
		System.out.println("权限足够可以进入");
		return "user";
		
	}
	
	
	

	
	@RequestMapping("clear")
	@RequiresPermissions("user:create")
	public String clearusercache(){
		RealmUtil r = new RealmUtil();
		r.clearCached();
		return "menu";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	

}
